const operationSheetId = Context.argv.sheetId;
const operationRecordId = Context.argv.id;
Application.Record.UpdateRecords({
    SheetId: operationSheetId,
    Records: [{
        id: operationRecordId,
        fields: { '脚本运行日志': '' }
    }]
});
const summarySheetName = '迭代资源预测占比';
const implementOccupy = 0.25;
const csOccupy = 0.15;
main();
function main() {
    let targetRowData = getTargetRecord(operationSheetId, operationRecordId);
    //* [测试代码]-start
    // let targetRowData = { '迭代列表': ['202409-1'], '类型': '迭代统计', '数据表名称': '2024迭代需求' }
    //* [测试代码]-end
    let interationList = targetRowData['迭代列表'];
    let sheetName = targetRowData['数据表名称'];
    let targetSheet = getSheetByName(sheetName);
    let targetSheetData = getSheetData(targetSheet.id);
    let summarySheet = getSheetByName(summarySheetName);
    let summarySheetData = getSheetData(summarySheet.id);
    let updateRecords = [];
    loop(interationList, (interation) => {
        const interationData = [];
        const interationSummaryData = [];
        loop(targetSheetData, record => {
            let { fields } = record;
            if (fields['迭代'] == interation) {
                interationData.push(record);
            }
        });
        loop(summarySheetData, record => {
            let { fields } = record;
            if (fields['研发迭代'] == interation) {
                interationSummaryData.push(record);
            }
        });
        loop(interationSummaryData, record => {
            const { id: recordId, fields } = record;
            let teamName = fields['负责小组'];
            let personNumber = fields['研发人数'];
            let workDays = fields['迭代天数'];
            let workEfficiency = fields['研发有效率'];
            let effectiveWorkDays = personNumber * workDays * workEfficiency;
            let implementDays = 0;
            let csDays = 0;
            let innerDays = 0;
            loop(interationData, interationRecord => {
                const { fields: taskFields } = interationRecord;
                let isParent = taskFields['是否是父任务'] == '1';
                let tags = taskFields['需求标签'] || [];
                let taskCategory = taskFields['需求分析类型'];
                let taskStatus = fields['需求状态'];
                let shouldIgnored = tags.includes('工时异常') || tags.includes('异常需求') || tags.includes('需求已移除') || isParent || ['经评估无法实现', '其他功能已解决'].includes(taskStatus);//过滤掉异常数据                
                let frontDays = taskFields['架构组前端评估人天'] || 0;
                let backEndDays = taskFields['架构组后端评估人天'] || 0;
                let testDays = taskFields['架构组测试评估人天'] || 0;
                let predicatedDays = frontDays + backEndDays + testDays;
                if (!shouldIgnored && teamName == taskFields['负责小组']) {
                    if (taskCategory == '交付') {
                        implementDays += predicatedDays;
                    } else if (taskCategory == '客成') {
                        csDays += predicatedDays;
                    } else {
                        innerDays += predicatedDays;
                    }
                }
            })
            updateRecords.push({
                id: recordId,
                fields: {
                    '交付人天': implementDays,
                    '客成人天': csDays,
                    '内部人天': innerDays,
                    '交付消耗率': implementDays / (effectiveWorkDays * implementOccupy),
                    '客成消耗率': csDays / (effectiveWorkDays * csOccupy),
                }
            })
        });
    })
    if (updateRecords.length > 0) {
        Application.Record.UpdateRecords({
            SheetId: summarySheet.id,
            Records: updateRecords,
        })
    }
}
/* 以下为通用方法,最新代码在lib目录下;受限于wps脚本运行的环境无法使用引用脚本,所以需要手动去更新以下脚本 */
function loop(list, cb) {
    for (let i = 0; i < list.length; i++) {
        cb(list[i], i);
    }
}
// 根据数据表名，查找表对象
function getSheetByName(name) {
    let sheets = Application.Sheet.GetSheets();
    let matchedSheet;
    for (let i = 0; i < sheets.length; i++) {
        if (sheets[i].name === name) {
            matchedSheet = sheets[i];
            break;
        }
    }
    if (!matchedSheet) console.warn('The specified table not existed!')
    return matchedSheet;
}
// 获取sheet中的数据
function getSheetData(sheetId) {
    let all = [];
    let offSet = null;
    while (all.length === 0 || offSet) {
        let recordInstance = Application.Record.GetRecords({ SheetId: sheetId, Offset: offSet });
        offSet = recordInstance.offset;
        all = all.concat(recordInstance.records);
    }
    return all;
}
function getTargetRecord(sheetId, recordId) {
    let result = Application.Record.GetRecord({ SheetId: sheetId, RecordId: recordId });
    return result.fields;
}